﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Aliyeye.Car.Net.Utility;

namespace Aliyeye.Car.Net.Dal
{
    public class Car_Insure
    {
        public Car_Insure(){ }
        public int Add(Model.Car_Insure model)
        {
            try
            {
                SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@ComName", SqlDbType.NVarChar,150),
					new SqlParameter("@ComAddr", SqlDbType.NVarChar,200),
					new SqlParameter("@ComTel", SqlDbType.NVarChar,50),
					new SqlParameter("@ComUser", SqlDbType.NVarChar,50),
					new SqlParameter("@CarCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@Project", SqlDbType.NVarChar,100),
					new SqlParameter("@StarTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@InputTime", SqlDbType.DateTime),
					new SqlParameter("@Mark", SqlDbType.NText)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.ComName;
                parameters[2].Value = model.ComAddr;
                parameters[3].Value = model.ComTel;
                parameters[4].Value = model.ComUser;
                parameters[5].Value = model.CarCode;
                parameters[6].Value = model.Price;
                parameters[7].Value = model.Project;
                parameters[8].Value = model.StarTime;
                parameters[9].Value = model.EndTime;
                parameters[10].Value = model.UserId;
                parameters[11].Value = model.InputTime;
                parameters[12].Value = model.Mark;

                int roleId = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Car_Insure_ADD", parameters);
                if (roleId > 0)
                {
                    return roleId;
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        public bool Update(Model.Car_Insure model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@ComName", SqlDbType.NVarChar,150),
					new SqlParameter("@ComAddr", SqlDbType.NVarChar,200),
					new SqlParameter("@ComTel", SqlDbType.NVarChar,50),
					new SqlParameter("@ComUser", SqlDbType.NVarChar,50),
					new SqlParameter("@CarCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@Project", SqlDbType.NVarChar,100),
					new SqlParameter("@StarTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@InputTime", SqlDbType.DateTime),
					new SqlParameter("@Mark", SqlDbType.NText)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.ComName;
            parameters[2].Value = model.ComAddr;
            parameters[3].Value = model.ComTel;
            parameters[4].Value = model.ComUser;
            parameters[5].Value = model.CarCode;
            parameters[6].Value = model.Price;
            parameters[7].Value = model.Project;
            parameters[8].Value = model.StarTime;
            parameters[9].Value = model.EndTime;
            parameters[10].Value = model.UserId;
            parameters[11].Value = model.InputTime;
            parameters[12].Value = model.Mark;

            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Car_Insure_Update", parameters);
            if (intResult > 0)
                return true;
            else
                return false;
        }
        public bool Delete(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;
            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Car_Insure_Delete", parameters);
            if (intResult > 0)
                return true;
            else
                return false;
        }
        public Model.Car_Insure GetModel(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            Model.Car_Insure model = new Model.Car_Insure();
            DataSet ds = SqlHelper.GetDataFormDataSet(CommandType.StoredProcedure, "Car_Insure_GetModel", parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.ComName = ds.Tables[0].Rows[0]["ComName"].ToString();
                model.ComAddr = ds.Tables[0].Rows[0]["ComAddr"].ToString();
                model.ComTel = ds.Tables[0].Rows[0]["ComTel"].ToString();
                model.ComUser = ds.Tables[0].Rows[0]["ComUser"].ToString();

                model.CarCode = ds.Tables[0].Rows[0]["CarCode"].ToString();
                
                if (ds.Tables[0].Rows[0]["Price"].ToString() != "")
                {
                    model.Price = decimal.Parse(ds.Tables[0].Rows[0]["Price"].ToString());
                }
                model.Project = ds.Tables[0].Rows[0]["Project"].ToString();
                if (ds.Tables[0].Rows[0]["StarTime"].ToString() != "")
                {
                    model.StarTime = DateTime.Parse(ds.Tables[0].Rows[0]["StarTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EndTime"].ToString() != "")
                {
                    model.EndTime = DateTime.Parse(ds.Tables[0].Rows[0]["EndTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(ds.Tables[0].Rows[0]["UserId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["InputTime"].ToString() != "")
                {
                    model.InputTime = DateTime.Parse(ds.Tables[0].Rows[0]["InputTime"].ToString());
                }
                model.Mark = ds.Tables[0].Rows[0]["Mark"].ToString();
                model.InputUserName = ds.Tables[0].Rows[0]["InputUserName"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,ComName,ComAddr,ComTel,ComUser,CarCode,Price,Project,StarTime,EndTime,UserId,InputTime,Mark ");
            strSql.Append(" FROM Car_Insure ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.Query(strSql.ToString());
        }
    }
}
